/* Copyright (c) 2011 by Mani Brundha Kattamuri, Haritha Subramanyam
 *
 * Project Info:  http://code.google.com/p/andriod-travelapp-csc780/wiki/Design
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 */
package com.example.TravelApp.search.common;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

/**
 * This class is responsible for plotting two overlay points and then drawing a
 * route between those locations. The draw method is overridden for this
 * purpose. The start and end of the route is marked based on the mode mode 1 :
 * Start location, indicated with blue color overlay mode 2 : the path mode 3 :
 * destination location, indicated with green color overlay
 * 
 * @author Haritha Subramanyam, Mani Brundha
 * 
 */
public class OverLayMapRoute extends Overlay {
	private GeoPoint gp1;
	private GeoPoint gp2;
	private int mRadius = 6;
	private int mode = 0;
	private int defaultColor;

	/**
	 * Constructs an overlayMapRoute for the specified geo points Also takes in
	 * the mode, which indicated whether it is a start point, or path or
	 * destination location
	 * 
	 * @param gp1
	 *            - the start location coordinates
	 * @param gp2
	 *            - the destination location coordinates
	 * @param mode
	 *            - values 1,2,3
	 */
	public OverLayMapRoute(GeoPoint gp1, GeoPoint gp2, int mode) {
		// GeoPoint is a int (6E)
		this.gp1 = gp1;
		this.gp2 = gp2;
		this.mode = mode;
		defaultColor = 999; // no defaultColor
	}

	/**
	 * Constructs an overlayMapRoute for the specified geo points Also takes in
	 * the mode, which indicated whether it is a start point, or path or
	 * destination location. This is an overloaded method and sets the default
	 * color
	 * 
	 * @param gp1
	 *            - the start location coordinates
	 * @param gp2
	 *            - the destination location coordinates
	 * @param mode
	 *            - values 1,2,3
	 * @param defaultColor
	 *            - color used to draw the path
	 */
	public OverLayMapRoute(GeoPoint gp1, GeoPoint gp2, int mode,
			int defaultColor) {
		this.gp1 = gp1;
		this.gp2 = gp2;
		this.mode = mode;
		this.defaultColor = defaultColor;
	}

	/**
	 * Returns the mode of the overlay object
	 * 
	 * @return mode - values 1,2,3
	 */

	public int getMode() {
		return mode;
	}

	/**
	 * draw method is overridden to be handled explicitly Circles through all
	 * the overlays and draws a small oval indicating the start or the
	 * destination points mode 1 : Start location, indicated with blue color
	 * overlay mode 2 : the path mode 3 : destination location, indicated with
	 * green color overlay
	 * 
	 * @param canvas
	 *            - canvas used to draw line
	 * @param mapView
	 *            - the mapview on which the route has to be drawn
	 * @param shadow
	 *            -
	 */
	@Override
	public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
			long when) {
		Projection projection = mapView.getProjection();
		if (shadow == false) {
			Paint paint = new Paint();
			paint.setAntiAlias(true);
			Point point = new Point();
			projection.toPixels(gp1, point);
			// mode=1&#65306;start
			if (mode == 1) {
				if (defaultColor == 999)
					paint.setColor(Color.BLUE);
				else
					paint.setColor(defaultColor);
				RectF oval = new RectF(point.x - mRadius, point.y - mRadius,
						point.x + mRadius, point.y + mRadius);
				// start point
				canvas.drawOval(oval, paint);

			}
			// mode=2&#65306;path
			else if (mode == 2) {
				if (defaultColor == 999)
					paint.setColor(Color.RED);
				else
					paint.setColor(defaultColor);
				Point point2 = new Point();
				projection.toPixels(gp2, point2);
				paint.setStrokeWidth(5);
				paint.setAlpha(120);
				canvas.drawLine(point.x, point.y, point2.x, point2.y, paint);
			}
			/* mode=3&#65306;end */
			else if (mode == 3) {
				/* the last path */

				if (defaultColor == 999)
					paint.setColor(Color.GREEN);
				else

					paint.setColor(defaultColor);
				Point point2 = new Point();
				projection.toPixels(gp2, point2);
				paint.setStrokeWidth(5);
				paint.setAlpha(120);
				canvas.drawLine(point.x, point.y, point2.x, point2.y, paint);
				RectF oval = new RectF(point2.x - mRadius, point2.y - mRadius,
						point2.x + mRadius, point2.y + mRadius);
				/* end point */
				paint.setAlpha(255);
				canvas.drawOval(oval, paint);
			}
		}
		return super.draw(canvas, mapView, shadow, when);
	}

}
